<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>角色管理</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
 <link rel="stylesheet" href="${ctx}/resources/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" type="text/css" href="${ctx}/resources/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/css/wu.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/css/icon.css" />
<script type="text/javascript" src="${ctx}/resources/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/resources/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${ctx}/resources/zTree/js/jquery.ztree.excheck.js"></script>
</head>

<body>
	 
	<!-- 查询条件开始 -->
	<div class="easyui-panel" data-options="title:'查询条件',iconCls:'icon-search'" style="width:100%;">
		<form id="searchFrm" method="get">
			<table width="100%" cellpadding="10" >
				<tr>
					<td width="33%">
					<input class="easyui-textbox" name="roleid"
							data-options="label:'角色ID:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					<td width="33%"> 
					<input class="easyui-textbox" name="rolename"
							data-options="label:'角色姓名:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
					
				</tr>
				<tr>
					<td width="33%">
					<input class="easyui-textbox" name="roledesc"
							data-options="label:'角色描述:',labelPosition:'left',labelAlign:'right'"
							style="width:80%;">
					</td>
				
					<td width="33%">
						<label class="textbox-label" style="text-align: right;">
        				角色是否可用:
        			</label>
           			 <input class="easyui-radiobutton" name="available" value="0">&nbsp;&nbsp;不可用&nbsp;&nbsp;&nbsp;&nbsp;
           			 <input class="easyui-radiobutton" name="available" value="1">&nbsp;&nbsp;可用
					</td>
				</tr>
				<tr>
					<td colspan="3" align="center">
						<a href="javascript:void(0)" class="easyui-linkbutton" onclick="doSearch()" data-options="iconCls:'icon-search'">查询</a>
						<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#searchFrm').form('clear')" data-options="iconCls:'icon-reload'">重置</a>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<!-- 查询条件结束 -->
	 <div style="height: 2px"></div>
	 <!-- 数据表格开始 -->
    <table id="table" class="easyui-datagrid" style="width:100%;height:300px">
    </table>
	<div id="tb">
	<a href="javascript:void(0)" class="easyui-linkbutton" id="addRole" data-options="iconCls:'icon-add',plain:true">添加角色</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="updateRole" data-options="iconCls:'icon-edit',plain:true">修改角色</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="deleteRole" data-options="iconCls:'icon-remove',plain:true">删除角色</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" id="selectRoleMenus" data-options="iconCls:'icon-page-excel',plain:true">分配菜单</a>
	</div>
	<!-- 数据表格结束  -->
	
	<!-- 添加和修改用户的弹出层 开始 -->
	<div id="dlg" class="easyui-dialog" style="width:500px" data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <div style="margin-bottom:10px">
                <input name="roleid" id="roleid" class="easyui-textbox" required="true" label="角色ID:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="rolename" class="easyui-textbox" required="true" label="角色姓名:" style="width:100%">
            </div>
            
            <div style="margin-bottom:10px">
                <input name="roledesc" class="easyui-textbox" required="true"  label="角色描述:" style="width:100%">
            </div>
             
             <div style="margin-bottom:10px">
             	<label class="textbox-label" style="text-align: left;">
        				角色性别:
        			</label>
           			 <input class="easyui-radiobutton" name="available" value="1">&nbsp;&nbsp;可用&nbsp;&nbsp;
           			 <input class="easyui-radiobutton" name="available" value="0">&nbsp;&nbsp;不可用&nbsp;&nbsp;
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveRole()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
	
	<!-- 添加和修改用户的弹出层 结束-->

<!-- 分配菜单弹出层 开始 -->
<div id="dlg-menus" class="easyui-dialog" style="width:500px" data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons-menus'">
<div class="ztree" id="treeMenus"></div>
</div>
<div id="dlg-buttons-menus">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveRoleMenus()" style="width:90px">确认分配</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>  
</div>
 <!-- 分配菜单的弹出层 结束-->
    
	<script type="text/javascript">
	//加载表格数据
	$("#table").datagrid({
		title:'角色列表',
		singleSelect:true,
		collapsible:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:'#tb',
        url:'${ctx}/role/loadAllRole.action',
        columns:[[
                  {field:'roleid',title:'角色ID',align:'center',width:100},
                  {field:'rolename',title:'角色姓名',align:'center',width:100},
                  {field:'roledesc',title:'角色描述',align:'center',width:100},
                  {field:'available',title:'角色是否可用',align:'center',width:100,formatter:function(value){
                	  if(value==0){
                 		 return "<font color=red>不可用</font>"; 
                 	  }else{
                 		  return "<font color=green>可用</font>"; 
                 	  }
                  }},
              ]]
       
	});
		//查询
		function doSearch(){
			var params=$("#searchFrm").serialize();
			$("#table").datagrid({
				url:'${ctx}/role/loadAllRole.action?'+params
			});
		};
		//打开添加用户的弹出层
		var url="";
		$("#addRole").click(function(){
			$("#dlg").dialog("open").dialog('setTitle','添加角色');
			$("#fm").form("clear");//清空表格
			$("#fm").form("load",{available:1});
			//设置id可读
			$("#roleid").textbox('readonly',false);
			url="${ctx}/role/addRole.action";
		});
		//打开修改的弹出层
		$("#updateRole").click(function(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			if(row){
				$("#dlg").dialog("open").dialog('setTitle','修改角色');
				//表单数据装载
				$("#fm").form("load",row);
				//设置roleid只读
				$("#roleid").textbox('readonly',true);
				url="${ctx}/role/updateRole.action";
			}else{
				$.messager.show({
					title:'提示',
					msg:'请选中操作行'
				});
			}
		});
		
		
		//保存
		function saveRole(){
			$.messager.progress();	// 显示进度条
			$('#fm').form('submit', {
				url: url,                                  //数据提交地址  user/addRole.action
				onSubmit: function(){
					var isValid = $(this).form('validate');//对表单里面的required:true和vailType的数据进行验证
					if (!isValid){
						$.messager.progress('close');	    // 如果验证失败关闭进度条
					}
					return isValid;	                  //返回验证结果
				},
				success: function(obj){               //如果提交成功之后回调的方法
					$.messager.progress('close');	  // 关闭进度条
                    var result = eval('('+obj+')');    //把obj的josn字符串转成js认识的json对象
					$.messager.show({
						title:'提示',
						msg:result.msg
					});
					$("#table").datagrid("reload");    //刷新数据表格
					$("#dlg").dialog("close");         //关闭弹出层
				}
			});
		}
		//删除用户
		$("#deleteRole").click(function(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			if(row){
				$.messager.confirm('提示','你确定要删除【'+row.rolename+'】这个角色吗?',function(r){
                    if (r){
                        $.post('${ctx}/role/deleteRole.action',{roleid:row.roleid},function(result){
                        	//var resu = eval('('+result+')');    为啥没用
                                $.messager.show({    
                                    title: '提示',
                                    msg: '删除成功'
                                });
                                $("#table").datagrid("reload");//刷新表格
                        },'json');
                    }
                });
			}else{
				$.messager.show({
					title:'提示',
					msg:'请选中操作行'
				});
			}
		});
	
		//tree的设置
		var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			check: {
				enable: true
			}
		};
		
		//弹出分配菜单的弹出层
		$("#selectRoleMenus").click(function(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			if(row){
				$("#dlg-menus").dialog("open").dialog("setTitle","分配【"+row.rolename+"】菜单");
				$.post("${ctx}/role/loadRoleSelectMenusTree.action",{roleid:row.roleid},function(zNodes){
					$.fn.zTree.init($("#treeMenus"), setting, zNodes);
				});
			}else{
				$.messager.show({
					title:'提示',
					msg:'请选中操作行'
				});
			}
		});
		//保存
		function saveRoleMenus(){
			//得到当前行
			var row=$("#table").datagrid('getSelected');
			var params="";//使用URL传到后台的数据
			var roleid=row.roleid;
			params="roleid="+roleid;
			var treeObj = $.fn.zTree.getZTreeObj("treeMenus");
			var nodes = treeObj.getCheckedNodes(true);
			for(var i=0;i<nodes.length;i++){
				var node=nodes[i];
				params+="&ids="+node.id;
			}
			$.post("${ctx}/role/saveRoleMenus.action?"+params,function(obj){
				$("#dlg-menus").dialog("close");
				$.messager.show({
					title:'提示',
					msg:obj.msg
				});
			});
			
		}
	
	</script>
</body>
</html>
